package controller.wechat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.Impl.WsBindingImpl;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by thinkjoy on 2017/8/31.
 * 获取员工号，并与openid对应
 * webapp应该在检测登陆成功后调用此接口
 */
@Controller
@RequestMapping("/ws/bind/")
public class WeChatBinding {

    @Autowired
    private WsBindingImpl wsBinding;

    /**
     * 绑定
     * @param data
     * @return
     */
    @RequestMapping(value = "binding",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public Map bindingwx(@RequestParam("workerid") String data){
        Map<String,Object> value=new HashMap<>();
        String openid=wsBinding.getOpenid();
        String flag=wsBinding.updateUserForOpenid(data,openid);
        if (flag.equals("0")){
            value.put("status",0);
            value.put("msg","绑定成功");
        }else if (flag.equals("1")){
            value.put("status",1);
            value.put("msg","绑定失败");
        }
        return value;
    }

    /**
     * 解绑
     * @return
     */
    @RequestMapping(value = "unbinding",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> unbindingWx(){
        Map<String,Object> value=new HashMap<>();
        String status=wsBinding.deleteUser();
        if (status.equals("0")){
            value.put("status",0);
            value.put("msg","解绑成功");
        }else {
            value.put("status",1);
            value.put("msg","解绑失败");
        }
        return value;
    }
}
